/*
 * MusicLabs, Copyright (C) 2008-2009 Felippe Ragghianti
 * http://code.google.com/p/musiclabs
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
 */


package br.com.music.scale;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import br.com.music.Interval;
import br.com.music.Pitch;

/**
 * A musical mode is based on well-known variants of the classic diatonic
 * scale. There are
 *
 * @version $LastChangedRevision$
 */
public class Mode extends DiatonicScale {

    private ModeType type;

    private String description;

    /**
     * Class constructor.
     *
     * @param tonic The tonic note.
     * @param type Mode type as in <code>{@link ModeType}</code> definitions.
     */
    public Mode(Pitch tonic, ModeType type) {
        super(tonic);
        this.type = type;
        description = tonic + " " + type;
        notes = createMode();
    }

    /**
     * Get Mode Type.
     *
     * @return The mode type.
     */
    public ModeType getType() {
        return type;
    }

    /**
     * Get mode description.
     *
     * @return
     */
    public String getDescription() {
        return description;
    }

    /**
     *
     * @return
     */
    private List<Pitch> createMode() {
        List<Interval> diatonicIntervals =
                Arrays.asList((Interval[])DiatonicScale.intervals.toArray().clone());
        Collections.rotate(diatonicIntervals, type.getRotationDistance());
        CromaticScale cromaticScale = new CromaticScale(tonic);
        List<Pitch> modeNotes = new ArrayList<Pitch>(8);
        int index = 0;
        for (Interval interval : diatonicIntervals) {
            modeNotes.add(cromaticScale.getNote(index));
            index= index + interval.toSemitones();
        }
        modeNotes.add(modeNotes.get(0));
        return modeNotes;
    }

}
